package com.guigu.handler;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义请求成功后跳转实现
 */
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    private String url;
    public MyAuthenticationSuccessHandler(String url) {
        this.url=url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        User user= (User) authentication.getPrincipal();
        System.out.println("账号:"+user.getUsername());
        System.out.println("密码(会显示Null安全性):"+user.getPassword());
        System.out.println("权限:"+user.getAuthorities());
        response.sendRedirect(url);
    }
}
